#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#define COUNT 3
#define MYNAME "mycdev"

struct cdev* cdev;
char kbuf[128] = { 0 };
int major = 0;
int minor = 0;
struct class* cls;
struct device* dev;

int mycdev_open(struct inode* inode, struct file* filp)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_read(struct file* filp,
    char __user* ubuf, size_t size, loff_t* offs)
{
    int ret;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);

    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret) {
        printk("copy data to user error\n");
        return -EINVAL;
    }

    return size;
}

ssize_t mycdev_write(struct file* filp,
    const char __user* ubuf, size_t size, loff_t* offs)
{
    int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret) {
        printk("copy data from user error\n");
        return -EINVAL;
    }

    return size;
}

int mycdev_close(struct inode* inode, struct file* filp)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    dev_t devno; // U32,设备号
    int ret = 0;
    int i = 0;

    // 1.分配对象
    cdev = cdev_alloc(); // cdev是结构体指针，用cdev_alloc函数申请内存
    if (cdev == NULL) {
        printk("cdev_alloc memory error\n");
        ret = -ENOMEM;
        goto ERR1;
    }

    // 2.对象初始化
    cdev_init(cdev, &fops);

    // 3.设备号的申请
    if (0 == major) {
        //动态
        ret = alloc_chrdev_region(&devno, minor, COUNT, MYNAME);
        if (ret) {
            printk("dynamic:request device number error\n");
            goto ERR2;
        }
        //以下为注册时函数参数填写设备号做兼容
        major = MAJOR(devno);
        minor = MINOR(devno);
    } else {
        //静态
        ret = register_chrdev_region(MKDEV(major, minor), COUNT, MYNAME);
        if (ret) {
            printk("static:request device number error\n");
            goto ERR2;
        }
    }

    // 4.字符设备驱动的注册
    ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
    if (ret) {
        printk("register chardev error\n");
        goto ERR3;
    }

    // 5.自动创建设备节点
    cls = class_create(THIS_MODULE, MYNAME);
    if (IS_ERR(cls)) {
        printk("class create error\n");
        ret = PTR_ERR(cls);
        goto ERR4;
    }
    for (i = minor; i < minor + 3; i++) {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(dev)) {
            printk("device create error\n");
            ret = PTR_ERR(dev);
            goto ERR5;
        }
    }

    return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
    for (--i; i >= minor; i--) {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    int i = 0;
    //注销设备节点
    for (i = minor; i < minor + 3; i++) {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
    //字符设备驱动的注销
    cdev_del(cdev);
    //设备号释放
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
    //释放cdev的内存空间
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");